package com.spring.dive.a05eraser;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class DiveEraserTest {
    public static void main(String[] args) {
        // 1.泛型类的泛型class是一样的
        ArrayList<String> strList = new ArrayList<String>();
        ArrayList<Integer> intList = new ArrayList<Integer>();
        System.out.println("--------是否相同:"+(strList.getClass() == intList.getClass()));

        // 2.无限制类型擦除，打印变量类型
        DiveEraser<String> eraser = new DiveEraser<String>();
        // 利用泛型获取DiveEraser字节码文件的class类对象
        Class<? extends DiveEraser> clz = eraser.getClass();
        // 获取所有成员变量
        Field[] declaredFields = clz.getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            Field field = declaredFields[i];
            // 打印结果：---->name:t type:class java.lang.Object
            System.out.println("---->name:"+field.getName()+" type:"+field.getType());
        }

        // 3.有限制类型擦除，<T extends Number>类型替换为上限类型Number
        DiveEraser2<Number> eraser2 = new DiveEraser2<Number>();
        // 利用泛型获取DiveEraser字节码文件的class类对象
        Class<? extends DiveEraser2> clz2 = eraser2.getClass();
        // 获取所有成员变量
        Field[] declaredFields2 = clz2.getDeclaredFields();
        for (int i = 0; i < declaredFields2.length; i++) {
            Field field = declaredFields2[i];
            // 打印结果：---->name:t type:class java.lang.Number
            System.out.println("---->name:"+field.getName()+" type:"+field.getType());
        }
        System.out.println();
        Method[] methods = clz2.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            // 打印结果：---->name:print type:class java.lang.Number
            System.out.println("---->name:"+method.getName()+" type:"+method.getReturnType());
        }
    }
    // 4.擦除参数中的类型
    // 不可以使用
//    public <T super Integer> void print(DiveEraser<? super T> clazz){
//        System.out.println("------->"+clazz);
//    }
    // 只能传递DiveEraser<Number>对象
    public static <T extends Number>  void print(DiveEraser<? super T> clazz){
        System.out.println("------->"+clazz);
    }
    public <T> void print2(DiveEraser<? super T> clazz){
        System.out.println("------->"+clazz);
    }
}
